﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AR.Drone.Client.Configuration;
using AR.Drone.Data;
using AR.Drone.Video;

namespace isDrone
{
    public class Video
    {
        private Emgu.CV.VideoWriter _writer;

        private VideoCodecType _codec = VideoCodecType.H264_720P;
        private VideoHelper VideoHelper = new VideoHelper();
        public VideoCodecType Codec
        {
            get
            {
                return _codec;
            }
            set
            {
                if (_codec!=value)

                _codec = value;
            }
        }

        private VideoPacketDecoderWorker _videoPacketDecoderWorker;
        private ISDrone _drone;
        public Video(ISDrone drone)
        { _drone = drone;
            _videoPacketDecoderWorker = new VideoPacketDecoderWorker(PixelFormat.BGR24, true, OnVideoPacketDecoded);
            _videoPacketDecoderWorker.Start();
           
          

        }

        public void Dispose()
        {
            _videoPacketDecoderWorker.Stop();
            _videoPacketDecoderWorker.Dispose();
        }

        public void CamSet(bool horizontal)
        {

            var configuration = new Settings();
            configuration.Video.Channel = VideoChannelType.Next;

            _drone.DroneClient.Send(configuration);

        }

        public void OnVideoPacketAcquired(VideoPacket packet)
        {
            if (_videoPacketDecoderWorker.IsAlive)
                _videoPacketDecoderWorker.EnqueuePacket(packet);
        }

        private uint _frameNumber;
        public void OnVideoPacketDecoded(VideoFrame frame)
        {
            if (NewFrame != null)
            {

                if (frame != null && _frameNumber != frame.Number)
                {
                    _frameNumber = frame.Number;
                    var b = VideoHelper.CreateBitmap(ref frame);
                  
                  
                   NewFrame(b);
                }
            }
        }

        private bool _rectord;
        public bool Record
        {
            set {
                _rectord = value;
            }
            get { return _rectord; }
        }


        public event NewFrameHandler NewFrame;

        public delegate void NewFrameHandler(System.Drawing.Bitmap bitmap);
    }
}
